/*
*
* 小区 楼栋 单元 控件
* */
Rsd.define('Rsd.Faith.controls.BuildingViewControl', {
    extend: 'Rsd.container.Component',
    requires: [
        'Rsd.control.Text',
        'Rsd.control.Tree',
        'Rsd.control.Grid',
        'Rsd.control.ToolBar'
    ],
    xtype: 'ctrl-building-view',
    layout: 'border',
    modelType:'',
    items:[
        {
            region: 'west',
            xtype:'tree',
            toolBar:{
                xtype:'toolBar',
                width:'100%',
                height:'100%',
                layout:'hbox',
                margin:'5 0 5 0',
                style:{lineHeight:'normal'},
                items:[
                    {
                        xtype:'text',
                        label:'小区',
                        width:230,
                        margin:'0 0 0 10'
                    },
                    {
                        xtype:'link',
                        style:{fontWeight:'normal'},
                        text:'[查找]',
                        margin:'0 5 15 0',
                        width:50,
                        align:'right',
                        handler:'btn_find'
                    }
                ]
            },
            showLine:false,
            showRoot:false,
            margin:'0 2 0 0',
            width:300
        },
        {
            xtype:'grid',
            region: 'center',
            pageIndexChanged:'pageIndexChanged',
            label:{
                xtype: 'grid-tool-bar',
                height:60,width:'100%',
                //searchColumns:[{name:'DanYuanHao',text:'单元'}],
                searchHandler:'loadLouDongRows'
            },
            //{"XiaoQuBianHao":"7600","LouDongHao":null,"DanYuanCount":1,"BianHaoCount":1,"Xiao_qu_bian_hao":"7600","Lou_dong_hao":null}
            columns: [
                {
                    "text": "序号",
                    "xtype": "index",
                    "width": 40,
                    "sortable": false
                },
                {
                    "xtype": "string",
                    "name": "XiaoQuBianHao",
                    "text": "小区编号",
                    "sortable": false,
                    "width": 80,
                    "nobr": true
                },
                {
                    "xtype": "string",
                    "name": "LouDongHao",
                    "text": "楼栋号",
                    "sortable": false,
                    "width": 100,
                    "format":"format_loudonghao",
                    "nobr": true
                },
                {
                    "xtype": "string",
                    "name": "DanYuanHao",
                    "text": "单元数",
                    "sortable": false,
                    "width": 100,
                    "precision": 0
                },
                {
                    "xtype": "number",
                    "name": "HuShu",
                    "text": "户数",
                    "precision": 0,
                    "sortable": false,
                    "width": 50,
                    "nobr": true
                },

                {
                    "name": "Remark",
                    "text": "备注",
                    "sortable": false,
                    "width": 150,
                    "nobr": true
                }
            ]

        }
    ],
    /*
     * */
    constructor: function (config) {
        config = config || {};
        Rsd.apply(this, config);

        this.on('afterrender', function () {


            this.getSchema();

            var _grid = this.items[1];

            _grid.dataSource = Rsd.app.getService('info.getloudonglist');
        });
    },
    btn_find:function()
    {
        this.loadData({});
    },
    loadData:function(args)
    {
        var _args = args||{};
        var me = this;
        //debugger;

        me.showLoading();
        var _tree =  this.items[0];
        var _store = Rsd.create('Rsd.Faith.stores.XiaoQuStore',{});
        _store.load({},function (data) {
            me.addNodes(_tree.root.id, data);
            _tree.build();
            me.closeLoading();
        });

    },
    format_loudonghao:function(row)
    {
        if(Rsd.isEmpty(row['LouDongHao']))
        {
            return [Rsd.text('-')];
        }
        var me = this;
        //report.exportexcel
        var s = Rsd.app.getService('report.exportexcel');

        var  btnExport = document.createElement('a');
        btnExport.href=s.server.url + '?xiaoqubianhao='+row['BianHao']+'&loudonghao='+row['LouDongHao'];
        btnExport.innerHTML = '发票导出';
        $(btnExport).click (function(){
            me.showLoading('正在下载...');
            setTimeout(function () {
                me.closeLoading();
            },2000);
        });


        return [Rsd.text(row['LouDongHao']),Rsd.newLine(),btnExport]
    },
    /*
     * */
    addNodes: function (pid, menus) {
        var _menu;
        var _node;
        var me = this;
        var _tree = this.items[0];
        var _list = menus.data.rows;
        for (var i in _list) {
            _menu = _list[i];

            _node = {id: pid + '__' + i,text:_menu.MingCheng, pid: pid, tag: _menu}

            if (!_menu.children
                || (Rsd.isArray(_menu.children) && _menu.children.length == 0)) {
                _node.dblClick = function () {

                    me.dataBind(this.tag);
                }
            }
            Rsd.apply(_node, _menu)

            _tree.addTreeNode(_node);

            delete  _node.children;
            if (_menu.children) {
                me.addNodes(_node.id, _menu.children);
            }
        }
    },
    /*
    * */
    dataBind:function(args)
    {
        var me = this;

        me.__xiao_qu_bian_hao = args.BianHao;
        me.loadLouDongRows();
    },
    /*
     * */
    getSchema:function()
    {
        var me = this;
        var grid = me.items[1];
        grid.openItem= function(record){me.openItem(record);};

    },
    /*
     * */
    pageIndexChanged:function pageIndexChanged(opt)
    {
        var me = this;
        me.loadLouDongRows();
    },
    /*
     * */
    loadLouDongRows:function loadLouDongRows( ) {

        var me = this;
        var  bian_hao= me.__xiao_qu_bian_hao;
        if(Rsd.isEmpty(bian_hao))
        {
            Rsd.alert("请选择小区");
            return;
        }

        var grid = me.items[1];
        Rsd.showWaiting(this.id,grid);
        grid.loadData({xiaoqubianhao:bian_hao},function(){

            Rsd.closeWaiting(me.id);
        });

    }

});

